package com.hans.Utils;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;

import java.util.List;

/**
 * Created by Administrator on 2016/7/4 0004.
 */
public class PackageManagerUtils {

    /**
     * 得到已经安装的浏览器
     * @param context
     * @return
     */
    public static List<ResolveInfo> getLocalBrowsers(Context context) {
        Intent intent = new Intent();
        intent.addCategory(Intent.CATEGORY_BROWSABLE);
        intent.setData(Uri.parse("http://"));
        List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent, PackageManager.GET_RESOLVED_FILTER);
        return list;
    }

    /**
     * 得到所有可以分享文本的APP
     * @param context
     * @return
     */
    public static List<ResolveInfo> getLocalShares(Context context) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType("text/plain");
        List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(intent,PackageManager.GET_RESOLVED_FILTER);
        return list;
    }

    /**
     * 启动指定包名的程序打开指定的数据
     * @param context
     * @param packageName
     * @param data
     */
    public void openApp(Context context,String packageName, Uri data) {
        try {
            Intent intent;
            intent = context.getPackageManager().getLaunchIntentForPackage(packageName);
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setData(data);
            context.startActivity(intent);
        } catch (Exception e) {
            // 在1.5及以前版本会要求catch(android.content.pm.PackageManager.NameNotFoundException)异常，该异常在1.5以后版本已取消。
            e.printStackTrace();
        }
    }

    /**
     * 使用默认的浏览器打开指定的链接
     * @param context
     * @param url
     */
    public void defaultBrowserOpenUrl(Context context, String url) {
        Intent intent = new Intent();
        intent.setAction("android.intent.action.VIEW");
        intent.setData(Uri.parse(url));
        context.startActivity(intent);
    }

    /**
     * 跳转到谷歌市场，如果没有安装谷歌市场APP，则用默认浏览器打开指定网址
     * @param context
     * @param uri
     */
    public void gotoGooglePlay(Context context,String uri){
        boolean androidMarketExists = false;
        try{
            ApplicationInfo info = context.getPackageManager().getApplicationInfo("com.android.vending", 0 );
            if(info.packageName.equals("com.android.vending")) androidMarketExists = true;
            else androidMarketExists = false;
        } catch(PackageManager.NameNotFoundException e ){
            androidMarketExists = false;
        }

        if(androidMarketExists){
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(uri));
            context.startActivity(intent);
        } else {
            //Toast.makeText(context, "sorry,Android Market doesn't exist!", Toast.LENGTH_LONG).show();
            defaultBrowserOpenUrl(context,uri);
        }
    }

}
